/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sdkj.dspservice.modules.sys.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sdkj.dspservice.common.base.entity.TreeEntity;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 菜单Entity
 *
 * @author tm
 * @version 2020-11-23
 */
@Data
@TableName(value = "sys_menu")
public class Menu extends TreeEntity<Menu> {

    /**
     * 名称
     */
    @NotBlank(message = "名称不能为空")
    private String name;
    /**
     * 链接
     */
    private String href;
    /**
     * 目标（ mainFrame、_blank、_self、_parent、_top）
     */
    private String target;
    /**
     * 图标
     */
    private String icon;
    /**
     * 是否在菜单中显示（1：显示；0：不显示）
     */
    private String isShow;
    /**
     * 权限标识
     */
    private String permission;

    public Menu() {
        super();
    }

    public Menu(String id) {
        super(id);
    }

    @JsonIgnore
    public static void makeTree(List<Menu> list, List<Menu> sourceList, String parentId, boolean cascade) {
        for (int i = 0; i < sourceList.size(); i++) {
            Menu e = sourceList.get(i);
            if (parentId.equals(e.getParentId())) {
                list.add(e);
                if (cascade) {
                    // 判断是否还有子节点, 有则继续获取子节点
                    for (int j = 0; j < sourceList.size(); j++) {
                        Menu child = sourceList.get(j);
                        if (child.getParentId().equals(e.getId())) {
                            makeTree(list, sourceList, e.getId(), true);
                            break;
                        }
                    }
                }
            }
        }
    }

    @JsonIgnore
    public static String getRootId() {
        return "0";
    }

}